﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Interfaces;
using ShopB2C.ViewModels;

namespace ShopB2C.SQLDataAccess
{
    public class Comment : IComment
    {

        #region Const Variables

        private const string _commentUpdate = "Comment_Update";
        private const string _commentDelete = "Comment_Delete";
        private const string _commentSelect = "Comment_Select";
        private const string _commentSelectList = "Comment_SelectList";
        private const string _commentCount = "Comment_Count";
        private const string _commentHot = "Comment_Hot";
        private const string _commentAudit = "Comment_Audit";
        private const string _commentProduct = "Comment_Product";

        #endregion

        public Comment(string connectionString)
        {
            DbBase.ConnectionString = connectionString;
        }

        #region Comment

        public void CommentUpdate(CommentEntity ce)
        {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@CommentId" , ce.CommentId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<String>("@Content" , 1500 , ce.Content).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<Int32>("@GradeValue" , ce.GradeValue).SqlDbType = SqlDbType.Int;
            sqlParams.Add<bool>("@IsAudit" , ce.IsAudit).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@ProductId" , ce.ProductId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@Uid" , ce.Uid).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@ReCommentId" , ce.ReCommentId).SqlDbType = SqlDbType.Int;
            SqlDbProcedure.ExecuteNonQuery(_commentUpdate , sqlParams.ToArray);
        }

        public void CommentDelete(string commentIds)
        {
            SqlDbProcedure.ExecuteNonQuery(_commentDelete ,
                DbParams<SqlParameter>.CreateParameter<string>("@CommentIds" , commentIds));
        }

        public void CommentAudit(string commentIds)
        {
            SqlDbProcedure.ExecuteNonQuery(_commentAudit ,
                DbParams<SqlParameter>.CreateParameter<string>("@CommentIds" , commentIds));
        }

        public CommentEntity GetCommentById(int commentId)
        {
            CommentEntity ce = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_commentSelect ,
                DbParams<SqlParameter>.CreateParameter<int>("@CommentId" , commentId));
            if (dr.HasRows)
            {
                if (dr.Read())
                {
                    ce = new CommentEntity()
                    {
                        CommentId = (int)dr["CommentId"] ,
                        Content = dr["Content"].ToString() ,
                        GradeValue = (int)dr["GradeValue"] ,
                        AddDate = (DateTime)dr["AddDate"] ,
                        ProductId = (int)dr["ProductId"] ,
                        Uid = (int)dr["Uid"] ,
                        ReCommentId = (int)dr["ReCommentId"] ,
                        ReplyNum = (int)dr["ReplyNum"] ,
                        IsAudit = (Boolean)dr["IsAudit"]
                    };
                }
            }
            dr.Close();
            dr.Dispose();
            return ce;
        }

        public List<CommentEntity> GetCommentList(CommentParameters cp) {
            List<CommentEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageSize", cp.PageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex", cp.PageIndex).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@ProductId", cp.ProductId).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@IsReply", (int)cp.IsReply).SqlDbType = SqlDbType.Bit;
            sqlParams.Add<Int32>("@SortField", (int)cp.SortField).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@GradeValue", (int)cp.GradeValue).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@Uid", cp.Uid).SqlDbType = SqlDbType.Int;
            if (cp.IsAudit.HasValue) {
                sqlParams.Add<bool>("@IsAudit", cp.IsAudit.Value).SqlDbType = SqlDbType.Bit;
            }
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_commentSelectList, sqlParams.ToArray);
            if (dr.HasRows) {
                listResult = new List<CommentEntity>();
                while (dr.Read()) {
                    cp.PageCount = (int)dr["PageCount"];
                    cp.RowsCount = (int)dr["RowsCount"];
                    listResult.Add(new CommentEntity() {
                        CommentId = (int)dr["CommentId"],
                        Content = dr["Content"].ToString(),
                        GradeValue = (int)dr["GradeValue"],
                        AddDate = (DateTime)dr["AddDate"],
                        ProductId = (int)dr["ProductId"],
                        Uid = (int)dr["Uid"],
                        ReCommentId = dr["ReCommentId"] is DBNull ? 0 : (int)dr["ReCommentId"],
                        UserName = dr["UserName"].ToString(),
                        NickName = dr["NickName"].ToString(),
                        LevelId = dr["LevelId"] is DBNull ? 0 : (int)dr["LevelId"],
                        LevelName = dr["LevelName"].ToString(),
                        LevelIcon = dr["LevelIcon"].ToString(),
                        UserFace = dr["Face"].ToString() ,
                        IsAudit = (Boolean)dr["IsAudit"],
                        ReplyNum = (int)dr["ReplyNum"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<CommentEntity> GetCommentHot(int top)
        {
            List<CommentEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@Top" , top).SqlDbType = SqlDbType.Int;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_commentHot , sqlParams.ToArray);
            if (dr.HasRows)
            {
                listResult = new List<CommentEntity>();
                while (dr.Read())
                {
                    listResult.Add(new CommentEntity()
                    {
                        Content = dr["Content"].ToString() ,
                        AverageGradeValue = (decimal)dr["GradeValue"] ,
                        ProductId = (int)dr["ProductId"] ,
                        AddDate = (DateTime)dr["AddDate"] ,
                        UserName = dr["UName"].ToString() ,
                        NickName = dr["NickName"].ToString() ,
                        LevelId = (int)dr["ReplyCount"] ,
                        LevelName = dr["ProductName"].ToString() ,
                        LevelIcon = dr["ImageName"].ToString()
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<CommentCount> GetCommentCount(int productId)
        {
            List<CommentCount> listResult = null;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_commentCount ,
                DbParams<SqlParameter>.CreateParameter<int>("@ProductId" , productId));
            if (dr.HasRows)
            {
                listResult = new List<CommentCount>();
                while (dr.Read())
                {
                    listResult.Add(new CommentCount()
                    {
                        Name = dr["Name"].ToString() ,
                        Sum = (int)dr["Sums"] ,
                        Avg = (decimal)dr["Avgs"] ,
                        Gv = (int)dr["Gv"] ,
                        Gvp = (decimal)dr["Gvp"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<ProductEntity> GetCommentProduct(bool? audit , int pageSize , int pageIndex , out int pageCount)
        {
            pageCount = 0;
            List<ProductEntity> listResult = new List<ProductEntity>();

            DbParams<SqlParameter> parameters = new DbParams<SqlParameter>();
            parameters.Add<int>("@PageSize" , pageSize).SqlDbType = SqlDbType.Int;
            parameters.Add<int>("@PageIndex" , pageIndex).SqlDbType = SqlDbType.Int;
            parameters.Add<bool?>("@IsAudit" , audit).SqlDbType = SqlDbType.Int;

            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_commentProduct , parameters.ToArray);
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new ProductEntity()
                    {
                        ProductId = (int)dr["ProductId"] ,
                        ProductName = dr["ProductName"].ToString() ,
                        SubName = dr["SubName"].ToString() ,
                        Description = dr["Description"].ToString() ,
                        KeyWords = dr["KeyWords"].ToString() ,
                        Price = (decimal)dr["Price"] ,
                        ClickCount = (int)dr["ClickCount"] ,
                        ReplyCount = (int)dr["ReplyCount"] ,
                        GradeValue = (decimal)dr["GradeValue"] ,
                        StockCount = (int)dr["StockCount"] ,
                        SalesCount = (int)dr["SalesCount"] ,
                        AddDateTime = (DateTime)dr["AddDateTime"] ,
                        IsActive = (bool)dr["IsActive"] ,
                        SaleDateTime = (DateTime)dr["SaleDateTime"] ,
                        DisCount = (double)dr["DisCount"] ,
                        ProductNo = dr["ProductNo"].ToString() ,
                        ClassId = (int)dr["ClassId"] ,
                        ImageName = dr["ImageName"].ToString() ,
                        BrandId = (int)dr["BrandId"] ,
                        Status = (ProductStatus)dr["Status"] ,
                        UnAuditCommentNum = (int)dr["UnAuditCommentNum"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();

            return listResult;
        }

        #endregion

    }
}
